//
//  YDTableView.m
//  ModelContrast
//
//  Created by Cluy on 15/6/19.
//  Copyright (c) 2015年 sunflowers. All rights reserved.
//

#import "YDTableView.h"
#import "YDTableCell.h"
#define sectionHeight 50
//#define cellHeight 40
@implementation YDTableView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
            self.dataSource = self;
            self.delegate = self;
            self.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
            self.showsVerticalScrollIndicator = NO;
    }
    return self;
}
-(void)awakeFromNib{
    self.dataSource = self;
    self.delegate = self;
    self.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.showsVerticalScrollIndicator = NO;
}
//边线补齐
-(void)willMoveToSuperview:(UIView *)newSuperview
{
    if ([self respondsToSelector:@selector(setSeparatorInset:)]) {
        [self setSeparatorInset:UIEdgeInsetsZero];
        
    }
    if ([self respondsToSelector:@selector(setLayoutMargins:)])  {
        [self setLayoutMargins:UIEdgeInsetsZero];
    }
}

#pragma mark -
#pragma mark Table view data source

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
    if (sectionTitle == nil) {
        return  nil;
    }
    
    UIView * sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, sectionHeight)];
    [sectionView setBackgroundColor:[UIColor clearColor]];
    
    UILabel * label = [[UILabel alloc] init];
    label.frame = CGRectMake(0, 0, tableView.bounds.size.width, sectionHeight);
    label.backgroundColor = [UIColor clearColor];
    label.font=[UIFont fontWithName:@"Helvetica-Bold" size:14];
    label.textColor = [UIColor blueColor];
    label.text = sectionTitle;
    
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0,sectionHeight-1 , tableView.bounds.size.width, 1)];
    lineView.backgroundColor = [UIColor blueColor];
    [sectionView addSubview:lineView];
    [sectionView addSubview:label];
    return sectionView;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    if ([_sectionList count]>0) {
          return [_sectionList count];
    }
    else{
        return 1;

    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return sectionHeight;
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if([_sectionList count]>0 && [_titleList count]>0) {
        return  [_titleList objectAtIndex:section];
    }else{
        return @"";
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
     if ([_sectionList count]>0) {
//         _keylist =[_sectionList objectAtIndex:section];
         return [[_sectionList objectAtIndex:section]count];
     }
     return [_keylist count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *CellIdentifier = [NSString stringWithFormat:@"YDTableCell%zd",indexPath.section];
    [self registerClass:[YDTableCell class] forCellReuseIdentifier:CellIdentifier];
    YDTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    NSUInteger row = [indexPath row];
    NSArray *tempArr =[NSArray array];
     if ([_sectionList count]>0) {
         tempArr = [_sectionList objectAtIndex:indexPath.section];
         cell.labe1.text = [tempArr objectAtIndex:row];
     }else{
         cell.labe1.text = [_keylist objectAtIndex:row];
     }
    return cell;
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
}
#pragma mark -
#pragma mark Table view delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([_sectionList count]>0) {
        selectKeyText =[[_sectionList objectAtIndex:indexPath.section]objectAtIndex:[indexPath row]];
    }else
    {
        selectKeyText = [_keylist objectAtIndex:[indexPath row]];
        selectValueText = [_valuelist objectAtIndex:[indexPath row]];
    }

    if(_getValuedelegate){
        [_getValuedelegate getKeyName:selectKeyText selectValue:selectValueText tableView:tableView];
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [_getValuedelegate tableViewDidScroll:scrollView];
}

@end
